<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{

    private $goods = null;
    private $cat = null;

    public function __construct(){
        parent::__construct();
        $this->goods = D('Goods');
        $this->cat = D('Cat');
    }

    public function goodsList(){
        //$goods = D('Goods');
        //$cat = D('Cat');
        //echo('<pre>');
        //print_r($goods);
        //$goodsData = $this->goods->field('goods_id,goods_name,goods_sn,shop_price,is_delete,is_best,is_new,is_hot,click_count,goods_number')->select();
        //$this->assign('goodData',$goodsData);
        //print_r($goodsData);
        $count      = $this->goods->count();
        $Page       = new \Think\Page($count,10);
        $show       = $Page->show();// 分页显示输出
        $goodsData = $this->goods->field('goods_id,goods_name,goods_sn,shop_price,is_delete,is_best,is_new,is_hot,click_count,goods_number')->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('goodData',$goodsData);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        //var_dump($show);
        //$this->display();



        $catlist = $this->cat->field('cat_id,cat_name,parent_id')->select();
        $alist = catList($catlist);

        //print_r($alist);
        //$catlist = $this->cat->field('cat_id,cat_name,parent_id')->where(['parent_id'=>0])->select();
        $this->assign('alist',$alist);
        //print_r($catlist);
        //print_r($this->goods->select());
        $this->display();
    }


    public function goodsAdd(){

        //查询商品分类
        $catlist = $this->cat->field('cat_id,cat_name,parent_id')->select();
        //调用商品无限极分类方法
        $alist = catList($catlist);
        //把商品分类的内容输出到模板
        $this->assign('alist',$alist);
        //$aa = $this->goods->create();
        if($_POST){
            // if($_FILES){
            //     $upload = new \Think\Upload();// 实例化上传类
            //     $upload->maxSize   =     3145728 ;// 设置附件上传大小
            //     $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
            //     $upload->rootPath  =      './Uploads/'; // 设置附件上传根目录
            //     // 上传单个文件
            //     $info   =   $upload->uploadOne($_FILES['goods_img1']);
            //     if(!$info) {// 上传错误提示错误信息
            //         $this->error($upload->getError());
            //     }else{// 上传成功 获取上传文件信息
            //         echo $info['savepath'].$info['savename'];
            //     }
            // }

            //  $data[] = $aa;
//$this->goods->data($aa)->add();
            if (!$this->goods->create()){
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($this->goods->getError());
            }else{
                if($this->goods->add()){
                    $this->success('商品添加成功');
                }else{
                    $this->error('商品添加失败');
                }
            }


            //echo '<script>alert("we")</script>';
        }else{
            $this->display();
        }

    }

    public function del(){

    }

    public function edit(){

        if($_POST){
            //$goods_id = $_GET['goods_id'];
            if (!$this->goods->create()){
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                exit($this->goods->getError());
            }else{
                //print_r($this->goods->create());
                $data = $this->goods->create();
                $source = $this->goods->where('goods_id='.$data['goods_id'])->save();
                //$this->goods->add();
                if($source){
                    $this->success('修改成功');
                }else{
                    $this->error('修改失败');
                }
            }
        }else{
            //查询商品分类
            $goods_id = I('goods_id');
            //print_r($goods_id);
            $catlist = $this->cat->field('cat_id,cat_name,parent_id')->select();
            //调用商品无限极分类方法
            $alist = catList($catlist);
            //把商品分类的内容输出到模板
            $this->assign('alist',$alist);

            $goodsdata = $this->goods->where('goods_id='.$goods_id)->select();
            //print_r($goodsdata);
            $this->assign('goodsdata',$goodsdata[0]);
            $this->display();
        }


    }
}
?>
